$(function () {
    var province_id_change = function (callback) {
        $("#city_id").html("<option value=''>所属城市</option>");
        $("#area_id").html("<option value=''>所属区县</option>");
        $.post("/web/get/city/by/province", {province_id: $("#province_id").val()}, function (result) {
            result = JSON.parse(result);
            result.citys.forEach(function (city) {
                $("#city_id").append("<option value=" + city.id + ">" + city.name + "</option>")
            });
            if (callback){
                callback();
            }
        })
    };
    var city_id_change = function (callback) {
        $("#area_id").html("<option value=''>所属区县</option>");
        $.post("/web/get/area/by/city", {city_id: $("#city_id").val()}, function (result) {
            result = JSON.parse(result);
            result.areas.forEach(function (area) {
                $("#area_id").append("<option value=" + area.id + ">" + area.name + "</option>")
            });
            if (callback){
                callback();
            }
        })
    };

    $("#origin_use_company").autocomplete({
        source: function (request, response) {
            $.post("/web/search/use/company", {"company_name": $("#origin_use_company").val()}, function (result) {
                result = JSON.parse(result);
                if (result.use_companies.length == 0){
                    $(".edit_content").val("");
                    $(".edit_content").attr("disabled", "disabled");
                }
                vals = [];
                result.use_companies.forEach(function (item) {
                    vals.push({id: item.id, label: item.name, value: item.name})
                });
                response(vals);
            })
        },
        select: function( event, ui ) {
            $.post("/web/get/use/company", {id: ui.item.id}, function (result) {
                result = JSON.parse(result);
                $("#name").val(result.name);
                $("#trust_code").val(result.trust_code);
                $("#province_id").val(result.province_id);
                province_id_change(function () {
                    $("#city_id").val(result.city_id);
                    city_id_change(function () {
                        $("#area_id").val(result.area_id);
                    });
                });
                $("#address_detail").val(result.address_detail);
                $("#linkman").val(result.linkman);
                $("#linkman_tel").val(result.linkman_tel);
                $("#linkman_phone").val(result.linkman_phone);
                $("#linkman_email").val(result.linkman_email);

                $("#use_company_id").val(ui.item.id);
                $(".edit_content").removeAttr("disabled");
            });
        },
    });

    $("#province_id").click(function () {
        if ($(this).change()){
            province_id_change();
        }
    });

    $("#city_id").click(function () {
        if ($(this).change()){
            city_id_change();
        }
    });

    $("#register_form").validate({
        rules: {
            origin_use_company: "required",
            name: "required",
            trust_code: "required",
            province_id: "required",
            city_id: "required",
            area_id: "required",
            address_detail: "required",
            linkman: "required",
            linkman_phone: {
                required: true,
                minlength: 11,
                maxlength: 11
            },
            linkman_tel: {
                required: true,
            },
            linkman_email: "required"
        },
        errorPlacement: function (err, element) {
        }
    });

    $(".btn-register").click(function () {
        if($("#register_form").valid()){
            data = {};
            $("#register_form").serializeArray().forEach(function (item) {
                data[item.name] = item.value;
            });
            $.post("/web/use/company/register", data, function (result) {
                result = JSON.parse(result);
                if (result.result){
                    bootbox.alert("注册成功！", function () {
                        window.location.href = "/web/login?type=use_company";
                    });
                }else {
                    bootbox.alert(result.error_msg);
                }

            })
        }
        return false
    });
});